﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using MetadataDocumentationGenerator.DelegatesHelpers;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;

namespace MetadataDocumentationGenerator.Controls
{
    /// <summary>
    /// User control for specifying options of generation
    /// </summary>
    public partial class Step4 : UserControl, IStepControl
    {
        #region Variables

        /// <summary>
        /// Generation settings
        /// </summary>
        private readonly GenerationSettings _settings;

        /// <summary>
        /// IOrganization service
        /// </summary>
        private readonly IOrganizationService _service;

        #endregion Variables

        #region Constructor

        /// <summary>
        /// Initializes a new instance of class Step4
        /// </summary>
        /// <param name="settings">Generation settings</param>
        /// <param name="service">IOrganization service</param>
        public Step4(GenerationSettings settings, IOrganizationService service)
        {
            _settings = settings;
            _service = service;

            InitializeComponent();

            var bwFillLanguage = new BackgroundWorker();
            bwFillLanguage.DoWork += BwFillLanguageDoWork;
            bwFillLanguage.RunWorkerAsync();

            InitializeSettings();
        }

       #endregion Constructor

        #region User control handlers

        private void PbExcelClick(object sender, EventArgs e)
        {
            rdbExcel.Checked = true;
        }

        private void PbWordClick(object sender, EventArgs e)
        {
            rdbWord.Checked = true;
        }

        private void PbCSV_Click(object sender, EventArgs e)
        {
            rdbCSV.Checked = true;
        }

        #endregion User control handlers

        #region Methods

        private void InitializeSettings()
        {
            chkAudit.Checked = _settings.AddAuditInformation;
            chkFLS.Checked = _settings.AddFieldSecureInformation;
            chkRequirement.Checked = _settings.AddRequiredLevelInformation;
            chkSummary.Checked = _settings.AddEntitiesSummary;
            chkVAF.Checked = _settings.AddValidForAdvancedFind;

            rdbWord.Checked = _settings.OutputDocumentType == Output.Word;
            rdbExcel.Checked = _settings.OutputDocumentType == Output.Excel;
            rdbCSV.Checked = _settings.OutputDocumentType == Output.Csv;
            rdoSureStepExcel.Checked = _settings.OutputDocumentType == Output.SureStepExcel;
            rdoSureStepWord.Checked = _settings.OutputDocumentType == Output.SureStepWord;

            foreach (var lc in cbbLanguage.Items.Cast<LanguageCode>().Where(lc => lc.Lcid == _settings.DisplayNamesLangugageCode))
            {
                cbbLanguage.SelectedItem = lc;
                break;
            }
        }

        void BwFillLanguageDoWork(object sender, DoWorkEventArgs e)
        {
            var request = new RetrieveProvisionedLanguagesRequest();
            var response = (RetrieveProvisionedLanguagesResponse)_service.Execute(request);

            foreach (var lc in response.RetrieveProvisionedLanguages.Select(lcid => new LanguageCode { Lcid = lcid, Label = CultureInfo.GetCultureInfo(lcid).EnglishName }))
            {
                ComboBoxDelegates.AddItem(cbbLanguage, lc);
            }

            if (ComboBoxDelegates.GetItems(cbbLanguage).Count > 0)
            {
                ComboBoxDelegates.SetSelectedIndex(cbbLanguage, 0);
            }
        }
        
        public GenerationSettings GetSettings()
        {
            _settings.AddAuditInformation = chkAudit.Checked;
            _settings.AddEntitiesSummary = chkSummary.Checked;
            _settings.AddFieldSecureInformation = chkFLS.Checked;
            _settings.AddRequiredLevelInformation = chkRequirement.Checked;
            _settings.DisplayNamesLangugageCode = cbbLanguage.SelectedItem != null ? ((LanguageCode)cbbLanguage.SelectedItem).Lcid : ((LanguageCode)cbbLanguage.Items[0]).Lcid;

            if (rdbCSV.Checked) _settings.OutputDocumentType = Output.Csv;
            if (rdbWord.Checked) _settings.OutputDocumentType = Output.Word;
            if (rdbExcel.Checked) _settings.OutputDocumentType = Output.Excel;
            if (rdoSureStepExcel.Checked) _settings.OutputDocumentType = Output.SureStepExcel;
            if (rdoSureStepWord.Checked) _settings.OutputDocumentType = Output.SureStepWord;

            return _settings;
        }

        #endregion Methods

    }
}
